package appli;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

/**
 * La classe qui cree une petite fenetre, elle nous permet de choisir le type d'index
 *  et type de modele de recherche qu'on va utiliser.
 * @author wchen
 *
 */
public class ModeleConfig extends JFrame implements ActionListener {
	
	private static final long serialVersionUID = 1L;
	private Interface gui;
	private String index;
	private String model;
	private boolean removeEmptyWords;
	
	private JPanel jp1;
	private JPanel jp2;
	
	private JLabel jl1;
	private JLabel jl2;
	private JLabel jl3;
	private JLabel jl4;
	
	private JComboBox jcb1;
	private JComboBox jcb2;
	private JComboBox jcb3;
	
	private JButton jb1;
	private JButton jb2;
	
	public ModeleConfig(Interface gui){
		super();
		this.setTitle("Setting search mode");
		this.setPreferredSize(new Dimension(400,220));
		this.setLayout(new BorderLayout());
		this.setLocation(400,100);
		this.setResizable(false);
		
		this.gui = gui;
		this.index = null;
		this.model = null;
		this.removeEmptyWords = false;
		
		jp1 = new JPanel();
		jp2 = new JPanel();
		
		jl1 = new JLabel("Please choose the type of index and search model:");
		jl1.setPreferredSize(new Dimension(300,30));
		jl2 = new JLabel("Type of Index:");
		jl2.setPreferredSize(new Dimension(160,30));
		jl3 = new JLabel("Search Model:");
		jl3.setPreferredSize(new Dimension(160,30));
		jl4 = new JLabel("Empty words:");
		jl4.setPreferredSize(new Dimension(160,30));
		
		String[] indexs = {"Normal","Stemming"};
		String[] models = {"Vector Model","ExBoolean Model"};
		String[] remove = {"Keep","Remove"};
		jcb1 = new JComboBox(indexs);
		jcb2 = new JComboBox(models);
		jcb3 = new JComboBox(remove);
		jcb1.addActionListener(this);
		jcb1.setActionCommand("jcb1");
		jcb2.addActionListener(this);
		jcb2.setActionCommand("jcb2");
		jcb3.addActionListener(this);
		jcb3.setActionCommand("jcb3");
		
		/* Pour JPanel No.1 */
		// Ajouter la premiere ligne
		jp1.add(jl1);
		// Ajouter la deuxieme ligne
		jp1.add(jl2);
		jp1.add(jcb1);
		// Ajouter la troisieme ligne
		jp1.add(jl3);
		jp1.add(jcb2);
		jp1.add(jl4);
		jp1.add(jcb3);
		jp1.setBackground(Color.WHITE);
		
		/* Pour JPanel No.2 */
		jb1 = new JButton("OK");
		jb1.addActionListener(this);
		jb1.setActionCommand("Confirm");
		jb2 = new JButton("Cancel");
		jb2.addActionListener(this);
		jb2.setActionCommand("Cancel");
		
		jp2.setLayout(new FlowLayout(SwingConstants.RIGHT));
		jp2.setBackground(Color.WHITE);
		jp2.add(jb1);
		jp2.add(jb2);
		
		this.add(jp1, BorderLayout.CENTER);
		this.add(jp2, BorderLayout.SOUTH);
		pack();
		setVisible(true);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		if(arg0.getActionCommand() == "jcb1"){
			JComboBox cb = (JComboBox) arg0.getSource();
			index = (String) cb.getSelectedItem();
			System.out.println(index);	
		}
		
		else if(arg0.getActionCommand() == "jcb2"){
			JComboBox cb = (JComboBox) arg0.getSource();
			model = (String) cb.getSelectedItem();
			System.out.println(model);	
		}
		
		else if(arg0.getActionCommand() == "jcb3"){
			JComboBox cb = (JComboBox) arg0.getSource();
			String test = (String) cb.getSelectedItem();
			if(test == "Remove"){
				removeEmptyWords = true;
			}
			else if(test == "Keep"){
				removeEmptyWords = false;
			}
			else{
				System.err.println("Error!");
			}
			System.out.println(test);	
		}
		
		// Construire l'index selon le type d'index
		else if(arg0.getActionCommand() == "Confirm"){
			System.out.println("Confirm");
			index = (String) jcb1.getSelectedItem();
			model = (String) jcb2.getSelectedItem();
			String test = (String) jcb3.getSelectedItem();
			if(test == "Remove"){
				removeEmptyWords = true;
			}
			else if(test == "Keep"){
				removeEmptyWords = false;
			}
			else{
				System.err.println("Error!");
			}
			
			this.gui.setTypeIndex(index);
			this.gui.setTypeModel(model);
			this.gui.setRemoveEmptyWords(removeEmptyWords);
			this.gui.getApplication().construireIndex(index,model,removeEmptyWords);
			this.dispose();
		}
		
		else if(arg0.getActionCommand() == "Cancel"){
			System.out.println("Cancel");
			this.dispose();
		}
	}

}
